package com.henu.ocr.controller;

import com.henu.ocr.entity.Sms;
import com.henu.ocr.model.SmsDTO;
import com.henu.ocr.service.SmsService;
import com.henu.ocr.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Tag(name = "消息管理接口")
@RequestMapping("/sms")
public class SmsController {

    @Resource
    private SmsService smsService;

    @Operation(summary = "创建消息")
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)
    public Result<?> createSms(@RequestBody Sms sms, @RequestHeader("token") String token) {
        try {
            smsService.addSms(sms, token);
            return Result.OK("消息创建成功");
        } catch (IllegalArgumentException e) {
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            return Result.Exception("服务器处理异常");
        }
    }

    @Operation(summary = "删除消息")
    @DeleteMapping("/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public Result<?> deleteSms(@PathVariable Integer id, @RequestHeader("token") String token) {
        try {
            smsService.deleteSms(id, token);
            return Result.OK(null);
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "更新消息")
    @PutMapping("/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Result<?> updateSms(@PathVariable Integer id, @RequestBody Sms sms, @RequestHeader("token") String token) {
        try {
            smsService.updateSms(id, sms, token);
            return Result.OK("消息更新成功");
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "获取所有消息")
    @GetMapping
    public Result<List<SmsDTO>> getAllSms(@RequestHeader("token") String token) {
        try {
            List<SmsDTO> data = smsService.getAllSms(token);
            return Result.OK(data.isEmpty() ? "暂无消息" : "消息获取成功", data);
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "获取消息详情")
    @GetMapping("/{id}")
    public Result<SmsDTO> getSmsById(@PathVariable Integer id, @RequestHeader("token") String token) {
        try {
            SmsDTO data = smsService.getSmsById(id, token);
            return data != null ?
                    Result.OK("消息详情获取成功", data) :
                    Result.error(404, "消息不存在");
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }

    @Operation(summary = "标记消息已读")
    @PostMapping("/{id}/read-status")
    @ResponseStatus(HttpStatus.OK)
    public Result<?> markAsRead(@PathVariable Integer id, @RequestHeader("token") String token) {
        try {
            smsService.markAsRead(id, token);
            return Result.OK("已标记为已读");
        } catch (Exception e) {
            return Result.Exception(e.getMessage());
        }
    }
}